/**
 * Created by cyf on 15-12-1.
 * 聊天窗口操作器
 */
(function() {
	qxchat.ChartWinHandler = ChartWinHandler;
	var EXTEND = null; //是否需要继承
	function ChartWinHandler() {
		if (EXTEND) {
			EXTEND.apply(this, arguments);
		}
		//打开聊天窗口
		this.open = function(loginName, userName) {
				//窗口是否存在
				var flag = false;
				for (var i = 0; i < global.chatWinArray.length; i++) {
					if (global.chatWinArray[i].loginName == loginName) {
						try {
							//测试窗口是否有效，如果不出异常表明有效
							global.chatWinArray[i].win.window.window.fromUserLoginName = global.currentUserLoginName;
							global.chatWinArray[i].win.show();
							global.chatWinArray[i].win.focus();
							flag = true;
						} catch (e) {
							//出异常后窗口集合中删除窗口对象
							global.chatWinArray.splice(i, 1);
						}
						break;
					}
				}
				//不存在则创建
				if (!flag) {
					var _gui = require('nw.gui');
					var url = 'chat.html';
					var chatWin = _gui.Window.open(
						url, {
							"title": userName,
							"icon": "logo.png",
							"toolbar": false,
							"frame": false,
							"width": 500,
							"height": 510,
							"position": "center",
							"min_width": 400,
							"min_height": 400,
							"transparent": true,
							"focus": true,
							"resizable": true
						});
					//传递给聊天窗口的信息
					chatWin.on('document-start', function() {
							chatWin.window.window.qxchat = qxchat;
							chatWin.window.window.toUserLoginName = loginName;
							chatWin.window.window.fromUserLoginName = global.currentUserLoginName;
							chatWin.window.window.toUserName = userName;
							chatWin.window.window.fromUserName = global.currentUserName;
						})
						//窗口对象放入窗口集合
					var userChatWin = new Object();
					userChatWin.loginName = loginName;
					userChatWin.win = chatWin;
					global.chatWinArray.push(userChatWin);
					return chatWin;
				}
			}
			//关闭窗口
		this.close = function(loginName) {
			for (var i = 0; i < global.chatWinArray.length; i++) {
				if (global.chatWinArray[i].loginName == loginName) {
					global.chatWinArray[i].win.close();
				}
			}
		}
	}
})();

/**
 * Created by cyf on 15-12-1.
 * 群组聊天窗口操作器
 */
(function() {
	qxchat.GroupChartWinHandler = GroupChartWinHandler;
	var EXTEND = null; //是否需要继承
	function GroupChartWinHandler() {
		if (EXTEND) {
			EXTEND.apply(this, arguments);
		}
		//打开聊天窗口
		this.open = function(groupId, groupName) {
				//窗口是否存在
				var flag = false;
				for (var i = 0; i < global.groupChatWinArray.length; i++) {
					if (global.groupChatWinArray[i].groupId == groupId) {
						try {
							//测试窗口是否有效，如果不出异常表明有效
							global.groupChatWinArray[i].win.window.window.fromUserLoginName = global.currentUserLoginName;
							global.groupChatWinArray[i].win.show();
							global.groupChatWinArray[i].win.focus();
							flag = true;
						} catch (e) {
							//出异常后窗口集合中删除窗口对象
							global.groupChatWinArray.splice(i, 1);
						}
						break;
					}
				}
				//不存在则创建
				if (!flag) {
					var _gui = require('nw.gui');
					var url = 'groupChat.html';
					var chatWin = _gui.Window.open(
						url, {
							"title": groupName,
							"icon": "logo.png",
							"toolbar": false,
							"frame": false,
							"width": 500,
							"height": 510,
							"position": "center",
							"min_width": 400,
							"min_height": 400,
							"transparent": true,
							"focus": true,
							"resizable": true
						});
					//传递给聊天窗口的信息
					chatWin.on('document-start', function() {
							chatWin.window.window.qxchat = qxchat;
							chatWin.window.window.groupId = groupId;
							chatWin.window.window.fromUserLoginName = global.currentUserLoginName;
							chatWin.window.window.groupName = groupName;
							chatWin.window.window.fromUserName = global.currentUserName;
						})
						//窗口对象放入窗口集合
					var userChatWin = new Object();
					userChatWin.groupId = groupId;
					userChatWin.win = chatWin;
					global.groupChatWinArray.push(userChatWin);
					return chatWin;
				}
			}
			//关闭窗口
		this.close = function(groupId) {
			for (var i = 0; i < global.groupChatWinArray.length; i++) {
				if (global.groupChatWinArray[i].groupId == groupId) {
					global.groupChatWinArray[i].win.close();
				}
			}
		}
	}
})();


/**
 * Created by cyf on 15-12-1.
 * 群组管理窗口操作器
 */
(function() {
	qxchat.GroupInfoWinHandler = GroupInfoWinHandler;
	var EXTEND = null; //是否需要继承
	function GroupInfoWinHandler() {
		if (EXTEND) {
			EXTEND.apply(this, arguments);
		}
		this._setbtn = $('#setBtn');
		this._windowsize = new qxchat.WindowSize();
		var self = this;
		this.open = function(node) {
			var _gui = require('nw.gui');
			var groupInfoWin = _gui.Window.open(
				'groupInfo.html', {
					"title": "群组管理",
					"icon": "logo.png",
					"toolbar": false,
					"frame": false,
					"min_width": 568,
					"min_height": 459,
					"width": 568,
					"height": 459,
					"position": "right",
					"min_width": 568,
					"min_height": 459,
					"transparent": true,
					"resizable": true
				}
			);
			//传递给聊天窗口的信息
			groupInfoWin.on('document-start', function() {
				groupInfoWin.window.window.qxchat = qxchat;
				if (node != null) {
					groupInfoWin.window.window.groupId = node.id;
				} else {
					groupInfoWin.window.window.groupId = null;
				}
			})
		}
	}
})();

(function() {
	qxchat.DefaultSetting = DefaultSetting;
	var EXTEND = null; //是否需要继承
	function DefaultSetting() {
		if (EXTEND) {
			EXTEND.apply(this, arguments);
		}
		this._setbtn = $('#setBtn');
		this._windowsize = new qxchat.WindowSize();
		var self = this;
		this._setbtn.click(function() {
			self._windowsize._gui.Window.open(
				'settings.html', {
					"title": "设置",
					"icon": "logo.png",
					"toolbar": false,
					"frame": false,
					"max_width": 568,
					"max_height": 459,
					"width": 568,
					"height": 459,
					"position": "right",
					"min_width": 568,
					"min_height": 459,
					"resizable": true
				}
			);
		});
	}
})();